<?php

/**
 * Created by PhpStorm.
 * User: qiliping
 * Date: 2016/12/7
 * Time: 上午10:52
 */
class Verification_code_model extends CI_Model
{
    /**
     * 获取用户最后一次发送的验证码信息
     * @param  [type] $phone [description]
     * @param  [type] $type  [description] 验证码类型（1注册2预约短信）
     * @return [type]        [description]
     */
    public function getLastCodeByPhone($phone,$type){
        $sql = "SELECT * FROM {$this->db->dbprefix}b_verification_code WHERE phone = '{$phone}' AND type={$type} order by create_on desc LIMIT 1";
        return $this->db->query($sql)->row_array();
    }

    /**
     * 插入验证码到数据库
     * @param  [type] $phone [description]
     * @param  [type] $code  [description]
     * @param  [type] $type  [description]
     * @return [type]        [description]
     */
    public function insertCodeLog($phone,$code,$type){
        $data = array(
            'type' =>  $type,
            'code' => $code,
            'phone' =>  $phone,
            'create_on' =>  date("Y-m-d H:i:s", time()),
            'expiration_time' =>  date("Y-m-d H:i:s", time()+10*60),
            'is_use' => 0);
        $result = $this->db->insert('b_verification_code', $data);
        return $result;
    }

}
